package com.wanbatv.wanba.ahitv.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.PopupWindow;

import com.umeng.analytics.MobclickAgent;

/**
 * Created by Forcs on 15/7/16.
 */
public class MonitorActivity extends FragmentActivity {

    private static final int MSG_CLEAR_INPUT = 1;
    private StringBuilder mInput = new StringBuilder();
    private PopupWindow mPopupWindow = null;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_CLEAR_INPUT:
                    mInput.delete(0, mInput.length());
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_0:
            case KeyEvent.KEYCODE_1:
            case KeyEvent.KEYCODE_2:
            case KeyEvent.KEYCODE_3:
            case KeyEvent.KEYCODE_4:
            case KeyEvent.KEYCODE_5:
            case KeyEvent.KEYCODE_6:
            case KeyEvent.KEYCODE_7:
            case KeyEvent.KEYCODE_8:
            case KeyEvent.KEYCODE_9:
                if (mHandler.hasMessages(MSG_CLEAR_INPUT)) {
                    mHandler.removeMessages(MSG_CLEAR_INPUT);
                }
                mInput.append(keyCode - KeyEvent.KEYCODE_0);
                Log.d("MonitorActivity", "@@@ onkeyup " + mInput.toString());
                if ("63211236".equals(mInput.toString())) {
                    showSystemInfo();
                    mHandler.sendEmptyMessage(MSG_CLEAR_INPUT);
                } else {
                    mHandler.sendEmptyMessageDelayed(MSG_CLEAR_INPUT, 3000);
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    public void showSystemInfo() {

        Log.d("MonitorActivity", "@@@ show system info");

        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            return;
        }

        onShowSystemInfo();

        if (mPopupWindow == null) {
            mPopupWindow = new PopupWindow(this);

            mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    mPopupWindow = null;
                }
            });
        }

    }

    public void onShowSystemInfo() {
    }

    private class ScanProcTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }
    }
}
